package com.qishenyuan.competition.vo;

import com.qishenyuan.competition.constant.UserConstant;
import com.qishenyuan.competition.model.Teamup;
import com.qishenyuan.competition.model.User;
import com.qishenyuan.competition.utils.AliOssUtil;
import com.qishenyuan.competition.utils.DateUtil;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author Jacky
 * @since 2025-03-03
 */
@Data
public class TeamUpBriefResp {
    private Integer id;

    private String userName;

    private String avatar;

    private String school;

    private String createTime;

    private List<String> pictures;

    private String title;

    private Integer tag;

    private Integer type;

    @Resource
    private AliOssUtil aliOssUtil;

    public static TeamUpBriefResp buildVo(Teamup teamup, User user){
        TeamUpBriefResp resp = new TeamUpBriefResp();
        resp.setId(teamup.getId());
        resp.setCreateTime(DateUtil.format(teamup.getCreateTime(), DateUtil.FORMAT_YYYY_MM_DD));
        List<String> imageUrls = Optional.ofNullable(teamup.getPictures())
                .map(TeamUpBriefResp::string2List)
                .orElseGet(Collections::emptyList)
                .stream()
                .map(AliOssUtil::generatePreSignedUrl)
                .collect(Collectors.toList());
        resp.setPictures(imageUrls);
        resp.setTitle(teamup.getTitle());
        resp.setTag(teamup.getTag());
        resp.setType(teamup.getType());
        if (!Objects.isNull(user)) {
            if (!StringUtils.isBlank(user.getNickName())) {
                resp.setUserName(user.getNickName());
            }
            if (UserConstant.DEFAULT_AVATAR.equals(user.getAvatar())) {
                resp.setAvatar(UserConstant.DEFAULT_AVATAR);
            }else {
                resp.setAvatar(AliOssUtil.generatePreSignedUrl(user.getAvatar()));
            }
            resp.setSchool(user.getSchool());
        }
        return resp;
    }

    // 把String转换成list，用“;”分隔开
    private static List<String> string2List(String str) {
        List<String> result = Arrays.asList(str.split(";"));
        result = result.stream().filter(s -> !s.isEmpty()).collect(Collectors.toList());
        return result;
    }
}
